package com.example.eurekaclient.web;

import com.example.eurekaclient.query.AuthQuery;
import com.nimbusds.jose.JOSEException;
import com.zhang.test.common.base.ResponseData;
import com.zhang.test.common.util.JjwtTokenUtil;
import com.zhang.test.common.util.TokenUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HomeController {


    @GetMapping("login")
    public String login(String name, String password) {

        Map<String, Object> map = new HashMap<>();
        map.put("userId", 10086);
        map.put("exp", System.currentTimeMillis() + 24 * 60 * 1000 * 60);

        String token = "";
        try {
            token = TokenUtil.getInstance().createToken(map);
        } catch (JOSEException e) {
            e.printStackTrace();
        }
        return token;
    }

    @RequestMapping("token")
    public ResponseData token(AuthQuery authQuery) {

        if (StringUtils.isEmpty(authQuery.getAccessKey()) || StringUtils.isEmpty(authQuery.getSecretKey())) {
            return ResponseData.failByParam("accessKey or secretKey is empty");
        }
        String token = JjwtTokenUtil.getInstance().getToken("10000");
        return ResponseData.ok(token);
    }

    @GetMapping("showToken")
    public JjwtTokenUtil.JWTResult showToken(String token) {
        return JjwtTokenUtil.getInstance().checkToken(token);
    }

}
